package com.fy.controller;

import com.fy.dao.UserMapper;
import com.fy.dao.User_infoMapper;
import com.fy.entity.U_co;
import com.fy.entity.User_info;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/userinfo")
public class User_infoController {
    String id="1";
    @Resource
    User_infoMapper userInfoMapper;

    @GetMapping("/getAll")
    public List<User_info> getAll() {
        return userInfoMapper.findAll();
    }

    @PostMapping("/insert")
    public String insert(String info_id, String user_id,
                      String nick_name, String gender,
                      String sign, String address,String user_avatar) {
        userInfoMapper.insert_info(id, user_id, nick_name, gender, sign, address,user_avatar);
        id=(Integer.valueOf(id)+1+"");
        return "200";//创建用户信息表成功
    }
    @PostMapping("/getInfo")
    public List<User_info> getInfo(String info_id){
        return userInfoMapper.getInfo(info_id);
    }

    //    修改用户信息
    @PostMapping("/updateinfo")
    public String updateinfo(String info_id, String nick_name,
                          String gender, String sign,
                          String address,String user_avatar,int jf) {
        List<User_info> all = userInfoMapper.findAll();
        for (User_info u : all) {
            if (gender == null) {
                if (u.getInfo_id().equals(info_id)) {
                    gender = u.getGender();
                }
            }
            if (nick_name == null) {
                if (u.getInfo_id().equals(info_id)) {
                    nick_name = u.getNick_name();
                }
            }
            if (sign == null) {
                if (u.getInfo_id().equals(info_id)) {
                    sign = u.getSign();
                }
            }
            if (address == null) {
                if (u.getInfo_id().equals(info_id)) {
                    address = u.getAddress();
                }
            }
            if (user_avatar==null){
                if (u.getInfo_id().equals(info_id)) {
                    user_avatar = u.getUser_avatar();
                }
            }
            if (String.valueOf(jf)!=null){
                if (u.getInfo_id().equals(info_id)){
                    jf=u.getJf()+jf;
                }
            }
            if (String.valueOf(jf)==null){
                if (u.getInfo_id().equals(info_id)) {
                    jf = u.getJf();
                }
            }

        }
        userInfoMapper.update_info(nick_name, gender, sign, address,jf,info_id);
        return "200";//修改用户信息成功
    }
}
